<template>
  <div v-show="visible">
    <div v-if="mark" class="k-toast-mask_transparent" @click.stop=""></div>
    <div v-if="type === 'txt'" class="k-toast k-toast-txt">
      <div class="content">{{ txt }}</div>
    </div>
    <div v-else-if="type === 'common'" class="k-toast k-toast-common">
      <div v-if="icon" class="k-toast-icon" :class="icon"></div>
      <div class="k-toast-content">{{ txt }}</div>
      <div class="dot-flashing"></div>
    </div>
    <div v-else-if="type === 'loading'" class="k-toast k-toast-loading">
      <div class="dot-flashing"></div>
      <div v-if="txt" class="k-toast-content">{{ txt }}</div>
      <div v-else class="k-toast-content">加载中...</div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "KToast",
    data() {
      return {
        visible: false,
        icon: "",
        type: "",
        mark: true,
        txt: "加载中...",
      }
    },
    methods: {
      show(options) {
        let map = {
          "account": "会员支付",
          "wx": "微信支付",
          "ali": "支付宝支付"
        };
        if (options) {
          this.icon = options.icon ? options.icon : "";
          this.txt = options.txt || (map[options.icon] ? map[options.icon] : "加载中...");
          this.mark = options.mark;
          if (options.type) {
            this.type = options.type;
          } else {
            this.type = "common";
          }
          if (options.time) {
            setTimeout(() => {
              this.visible = false;
            }, options.time)
          }
        }
        this.visible = true;
      },
      message(txt, time) {
        this.type = 'txt';
        this.txt = txt;
        this.mark = false;
        this.visible = true;
        if (!time) {
          time = 1500
        }
        setTimeout(() => {
          this.visible = false;
        }, time)
      },
      showMark() {
        this.type = 'mark';
        this.mark = true;
        this.visible = true;
      },
      hide() {
        this.visible = false;
        this.mark = true;
      },
    }
  }
</script>

<style lang="postcss" scoped>
  .k-toast-mask_transparent {
    position: fixed;
    z-index: 2002;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
  }

  .k-toast {
    position: fixed;
    z-index: 5000;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.5);
    text-align: center;
  }

  .k-toast-txt {
    max-width: 500px;
    line-height: 80px;
    color: #f5f5f5;
    font-size: 28px;
    text-align: center;
    padding: 0 40px;
    border-radius: 6px;
  }

  .k-toast-common {
    color: #FFFFFF;
    width: 400px;
    height: 334px;
    border-radius: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    padding: 68px 0;
  }
  .k-toast-loading {
    color: #FFFFFF;
    width: 250px;
    //height: 240px;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 88px 0;
    & .k-toast-content {
      margin-top: 26px;
      font-size: 24px;
      color: #ddd;
    }
  }

  .k-toast-icon {
    background-repeat: no-repeat;
    background-size: 100% 100%;
    width: 80px;
  }

  .k-toast-content {
    font-size: 32px;
  }

  .dot-flashing {
    position: relative;
    width: 20px;
    height: 20px;
    border-radius: 10px;
    background-color: #fff;
    color: #fff;
    animation: dotFlashing 0.8s infinite linear alternate;
    animation-delay: 0.4s;
  }

  .dot-flashing::before, .dot-flashing::after {
    content: '';
    display: inline-block;
    position: absolute;
    top: 0;
  }

  .dot-flashing::before {
    left: -30px;
    width: 20px;
    height: 20px;
    border-radius: 10px;
    background-color: #fff;
    color: #fff;
    animation: dotFlashing 0.8s infinite alternate;
    animation-delay: 0s;
    content: ' ';
  }

  .dot-flashing::after {
    left: 30px;
    width: 20px;
    height: 20px;
    border-radius: 10px;
    background-color: #fff;
    color: #fff;
    animation: dotFlashing 0.8s infinite alternate;
    animation-delay: 0.8s;
    content: ' ';
  }


  @keyframes dotFlashing {
    0% {
      background-color: #fff;
    }
    50%,
    100% {
      background-color: #999;
    }
  }

</style>
